<template>
  <div id="main">
    <h2>学生管理</h2>
    <el-button
      type="primary"
      style="margin: 20px"
      @click="handleOpen('添加孩子', 1)"
    >添加学生信息</el-button>
    <div id="table-area">
      <el-table
        v-loading="listLoading"
        :data="list.slice((currentPage - 1) * pagesize, currentPage * pagesize)"
        element-loading-text="Loading"
        border
        fit
        highlight-current-row
      >
        <el-table-column label="序号" type="index" align="center">
          <template scope="scope">
            <span>{{ (currentPage - 1) * pagesize + scope.$index + 1 }}</span>
          </template>
        </el-table-column>

        <el-table-column align="center" prop="studentName" label="孩子姓名" />

        <el-table-column align="center" prop="studentSex" label="孩子性别" :formatter="showSex" />

        <el-table-column align="center" prop="studentNation" label="孩子民族" />

        <el-table-column
          align="center"
          prop="studentLanguageEnv"
          label="语言环境"
        />

        <el-table-column align="center" label="操作">
          <template slot-scope="Scope">
            <el-button
              type="primary"
              icon="el-icon-edit"
              @click="handleOpen('修改孩子', 2, Scope.row.studentId)"
            >编辑</el-button>
            <el-button
              type="danger"
              icon="el-icon-delete"
              @click="handleDelete(Scope.row.studentId, Scope.row.studentName)"
            >删除</el-button>
          </template>
        </el-table-column>
      </el-table>

      <div id="pages">
        <el-pagination
          :current-page="currentPage"
          :page-sizes="[5, 10]"
          :page-size="pagesize"
          layout="total, sizes,prev, pager, next"
          :total="list.length"
          prev-text="上一页"
          next-text="下一页"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
        />
      </div>
    </div>
    <el-dialog
      title=""
      :close-on-click-modal="false"
      :visible.sync="is_add_train"
      :before-close="handleClose_t"
    >
      <el-form ref="train" :model="train" label-position="top" :rules="rules">
        <el-form-item prop="effect" label="效果" :label-width="formLabelWidth">
          <el-input v-model="train.effect" autocomplete="off" />
        </el-form-item>
        <el-form-item
          prop="orgName"
          label="机构名称"
          :label-width="formLabelWidth"
        >
          <el-input v-model="train.orgName" autocomplete="off" />
        </el-form-item>
        <el-form-item
          prop="staffId"
          label="记录工作人员"
          :label-width="formLabelWidth"
        >
          <el-input v-model="train.staffId" autocomplete="off" />
        </el-form-item>
        <el-form-item
          prop="timeStart"
          label="开始时间"
          :label-width="formLabelWidth"
        >
          <el-date-picker
            v-model="train.timeStart"
            type="date"
            placeholder="选择日期"
            format="yyyy 年 MM 月 dd 日"
            value-format="yyyy-MM-dd"
          />
        </el-form-item>
        <el-form-item
          prop="timeEnd"
          label="结束时间"
          :label-width="formLabelWidth"
        >
          <el-date-picker
            v-model="train.timeEnd"
            type="date"
            placeholder="选择日期"
            format="yyyy 年 MM 月 dd 日"
            value-format="yyyy-MM-dd"
          />
        </el-form-item>
        <el-form-item
          prop="orgName"
          label="训练机构"
          :label-width="formLabelWidth"
        >
          <el-input
            v-model="train.orgName"
            autocomplete="off"
          />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="handleClose_t">取 消</el-button>
        <el-button
          v-if="editType == 1"
          type="primary"
          @click="handleAddTrain('train')"
        >添 加</el-button>
        <el-button
          v-if="editType == 2"
          type="primary"
          @click="handleUpdateTrain('train')"
        >修 改</el-button>
      </div>
    </el-dialog>
    <el-dialog
      title=""
      :close-on-click-modal="false"
      :visible.sync="is_add_mother"
      :before-close="handleClose_m"
    >
      <el-form ref="mother" :model="mother" label-position="top" :rules="rules">
        <el-form-item
          prop="parentAdd"
          label="家庭住址"
          :label-width="formLabelWidth"
        >
          <el-input v-model="mother.parentAdd" autocomplete="off" />
        </el-form-item>
        <el-form-item
          prop="parentBirthtoString"
          label="母亲生日"
          :label-width="formLabelWidth"
        >
          <el-date-picker
            v-model="mother.parentBirthtoString"
            type="date"
            placeholder="选择生日"
            format="yyyy 年 MM 月 dd 日"
            value-format="yyyy-MM-dd"
          />
        </el-form-item>
        <el-form-item
          prop="parentCall"
          label="母亲电话"
          :label-width="formLabelWidth"
        >
          <el-input v-model="mother.parentCall" autocomplete="off" />
        </el-form-item>
        <el-form-item
          prop="parentEducationalLevel"
          label="母亲文化水平"
          :label-width="formLabelWidth"
        >
          <el-input
            v-model="mother.parentEducationalLevel"
            autocomplete="off"
          />
        </el-form-item>
        <el-form-item
          prop="parentEmail"
          label="母亲邮箱"
          :label-width="formLabelWidth"
        >
          <el-input v-model="mother.parentEmail" autocomplete="off" />
        </el-form-item>
        <el-form-item
          prop="parentName"
          label="母亲姓名"
          :label-width="formLabelWidth"
        >
          <el-input v-model="mother.parentName" autocomplete="off" />
        </el-form-item>
        <el-form-item
          prop="parentOccupation"
          label="母亲职业"
          :label-width="formLabelWidth"
        >
          <el-input v-model="mother.parentOccupation" autocomplete="off" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="handleClose_m">取 消</el-button>
        <el-button
          type="primary"
          @click="handleAddMother('mother')"
        >添 加</el-button>
      </div>
    </el-dialog>
    <el-dialog
      title=""
      :close-on-click-modal="false"
      :visible.sync="is_add_father"
      :before-close="handleClose_f"
    >
      <el-form ref="father" :model="father" label-position="top" :rules="rules">
        <el-form-item
          prop="parentAdd"
          label="家庭住址"
          :label-width="formLabelWidth"
        >
          <el-input v-model="father.parentAdd" autocomplete="off" />
        </el-form-item>
        <el-form-item
          prop="parentBirthtoString"
          label="父亲生日"
          :label-width="formLabelWidth"
        >
          <el-date-picker
            v-model="father.parentBirthtoString"
            type="date"
            placeholder="选择生日"
            format="yyyy 年 MM 月 dd 日"
            value-format="yyyy-MM-dd"
          />
        </el-form-item>
        <el-form-item
          prop="parentCall"
          label="父亲电话"
          :label-width="formLabelWidth"
        >
          <el-input v-model="father.parentCall" autocomplete="off" />
        </el-form-item>
        <el-form-item
          prop="parentEducationalLevel"
          label="父亲水平"
          :label-width="formLabelWidth"
        >
          <el-input
            v-model="father.parentEducationalLevel"
            autocomplete="off"
          />
        </el-form-item>
        <el-form-item
          prop="parentEmail"
          label="父亲邮箱"
          :label-width="formLabelWidth"
        >
          <el-input v-model="father.parentEmail" autocomplete="off" />
        </el-form-item>
        <el-form-item
          prop="parentName"
          label="父亲姓名"
          :label-width="formLabelWidth"
        >
          <el-input v-model="father.parentName" autocomplete="off" />
        </el-form-item>
        <el-form-item
          prop="parentOccupation"
          label="父亲职业"
          :label-width="formLabelWidth"
        >
          <el-input v-model="father.parentOccupation" autocomplete="off" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="handleClose_f">取 消</el-button>
        <el-button
          type="primary"
          @click="handleAddFather('father')"
        >添 加</el-button>
      </div>
    </el-dialog>
    <el-dialog
      title=""
      :close-on-click-modal="false"
      :visible.sync="dialogFormVisible"
      :before-close="handleClose"
    >
      <el-form ref="child" :model="child" label-position="top" :rules="rules">
        <el-form-item prop="studentName" label="学生姓名" :label-width="formLabelWidth">
          <el-input v-model="child.studentName" autocomplete="off" />
        </el-form-item>

        <el-form-item prop="studentSex" label="性别" :label-width="formLabelWidth">
          <el-select v-model="child.studentSex" placeholder="学生性别">
            <el-option key="0" label="女" value="0" />
            <el-option key="1" label="男" value="1" />
          </el-select>
        </el-form-item>

        <el-form-item label="学生头像">
          <el-upload
            class="avatar-uploader"
            name="files"
            :action="url"
            :show-file-list="false"
            :on-success="handleAvatarSuccess"
            :before-upload="beforeAvatarUpload"
          >
            <img v-if="imageUrl" :src="imageUrl" class="avatar">
            <i v-else class="el-icon-plus avatar-uploader-icon" />
            <div slot="tip" class="el-upload__tip">只能上传jpg/png文件，且不超过2MB</div>
          </el-upload>
          <!-- <img v-if="type==2" :src="org.orgImg" width="200px" height="120px"> -->
        </el-form-item>

        <el-form-item prop="studentNation" label="学生民族" :label-width="formLabelWidth">
          <el-input v-model="child.studentNation" autocomplete="off" />
        </el-form-item>

        <el-form-item prop="studentLanguageEnv" label="语言环境" :label-width="formLabelWidth">
          <el-input v-model="child.studentLanguageEnv" autocomplete="off" />
        </el-form-item>

        <el-form-item prop="studentBirth" label="出生日期" :label-width="formLabelWidth">
          <el-date-picker
            v-model="child.studentBirth"
            type="date"
            placeholder="选择日期"
            format="yyyy 年 MM 月 dd 日"
            value-format="yyyy-MM-dd"
          />
        </el-form-item>

        <el-form-item prop="studentJoinCenter" label="进入中心日期" :label-width="formLabelWidth">
          <el-date-picker
            v-model="child.studentJoinCenter"
            type="date"
            placeholder="选择日期"
            format="yyyy 年 MM 月 dd 日"
            value-format="yyyy-MM-dd"
          />
        </el-form-item>

        <el-form-item prop="studentAddress" label="家庭住址" :label-width="formLabelWidth">
          <el-input v-model="child.studentAddress" autocomplete="off" />
        </el-form-item>

        <el-form-item prop="studentPhone" label="联系电话" :label-width="formLabelWidth">
          <el-input v-model="child.studentPhone" autocomplete="off" />
        </el-form-item>

        <el-form-item label="父亲姓名" :label-width="formLabelWidth">
          <el-form :inline="true" class="demo-form-inline">
            <el-form-item prop="studentFatherId">
              <el-select v-model="child.studentFatherId" filterable placeholder="请选择">
                <el-option
                  v-for="f in father"
                  :key="f.parentId"
                  :label="f.String"
                  :value="f.parentId"
                />
              </el-select>
            </el-form-item>
            <el-form-item>
              <el-button type="primary" @click="addF(1, 1)">添加</el-button>
            </el-form-item>
          </el-form>
        </el-form-item>

        <el-form-item prop="studentMotherId" label="母亲姓名" :label-width="formLabelWidth">
          <el-form :inline="true" class="demo-form-inline">
            <el-form-item prop="studentMotherId">
              <el-select v-model="child.studentMotherId" filterable placeholder="请选择">
                <el-option
                  v-for="f in mother"
                  :key="f.parentId"
                  :label="f.String"
                  :value="f.parentId"
                />
              </el-select>
            </el-form-item>
            <el-form-item>
              <el-button type="primary" @click="addM(1, 1)">添加</el-button>
            </el-form-item>
          </el-form>
        </el-form-item>

        <el-form-item prop="studentCare" label="主要带养人" :label-width="formLabelWidth">
          <el-input v-model="child.studentCare" autocomplete="off" />
        </el-form-item>

        <el-form-item prop="studentAccompanyTrainer" label="陪同训练人" :label-width="formLabelWidth">
          <el-input v-model="child.studentAccompanyTrainer" autocomplete="off" />
        </el-form-item>

        <el-form-item prop="studentBrothers" label="有无兄弟姐妹" :label-width="formLabelWidth">
          <el-select v-model="child.studentBrothers" placeholder="请选择">
            <el-option key="0" label="无" value="0" />
            <el-option key="1" label="有" value="1" />
          </el-select>
        </el-form-item>

        <el-form-item v-if="child.studentBrothers == 1" prop="brothersSituation" label="兄弟姐妹情况" :label-width="formLabelWidth">
          <el-input v-model="child.brothersSituation" autocomplete="off" />
        </el-form-item>

        <el-form-item prop="studentTreatHospital" label="就诊医院名称" :label-width="formLabelWidth">
          <el-input v-model="child.studentTreatHospital" autocomplete="off" />
        </el-form-item>

        <el-form-item prop="studentTreatCurrentDiagnosis" label="目前诊断" :label-width="formLabelWidth">
          <el-input v-model="child.studentTreatCurrentDiagnosis" autocomplete="off" />
        </el-form-item>

        <el-form-item prop="studentTreatPastTrainingHistoryId" label="既往训练史" :label-width="formLabelWidth">
          <el-input v-model="child.studentTreatPastTrainingHistoryId" autocomplete="off" />
        </el-form-item>

        <el-form-item prop="studentTreatTrainingAge" label="起训年龄" :label-width="formLabelWidth">
          <el-input v-model="child.studentTreatTrainingAge" autocomplete="off" />
        </el-form-item>

        <el-form-item prop="studentTreatOtherDiseases" label="其他疾病" :label-width="formLabelWidth">
          <el-input v-model="child.studentTreatOtherDiseases" autocomplete="off" />
        </el-form-item>

        <el-form-item prop="studentTreatAttention" label="训练注意事项" :label-width="formLabelWidth">
          <el-input v-model="child.studentTreatAttention" autocomplete="off" />
        </el-form-item>
        <el-form>
          <label>既往训练机构记录</label>
          <!-- <el-button style="margin-left:100px">+</el-button> -->
          <el-button
            icon="el-icon-plus"
            circle
            size="small"
            style="margin-left: 20px"
            @click="addItem()"
          />
          <!-- @click="addT(1)" -->
          <el-form-item>
            <el-table :data="train" style="width: 100%">
              <el-table-column prop="orgName" label="名称" width="320" />
              <el-table-column prop="timeStart" label="时间" width="320" />
              <el-table-column prop="effect" label="效果" />
              <el-table-column align="center" label="操作">
                <template slot-scope="Scope">
                  <el-button
                    type="primary"
                    icon="el-icon-edit"
                    circle
                    @click="
                      handleOpenTrain(
                        '修改记录',
                        2,
                        Scope.row.trainingInstitutionRecordId
                      )
                    "
                  />
                  <el-button
                    type="danger"
                    icon="el-icon-delete"
                    circle
                    @click="
                      handleDeleteTrain(Scope.row.trainingInstitutionRecordId)
                    "
                  />
                </template>
              </el-table-column>
            </el-table>
          </el-form-item>
        </el-form>
        <el-form
          ref="form"
          :rules="formRules"
          :inline="true"
          :model="form"
          label-width="80px"
        >
          <!-- 固定项目 -->
          <div v-for="(item, index) in form.dynamicItem" :key="index">
            <el-form-item
              label="机构名称"
              :prop="'dynamicItem.'+ index +'.orgName'"
              :rules="{
                required: true,
                message: '名称不能为空',
                trigger: 'blur',
              }"
            >
              <el-input
                v-model="item.orgName"
                style="width: 150px"
              />
            </el-form-item>
            <el-form-item
              label="进入时间"
              :prop="'dynamicItem.'+ index +'.timeStart'"
              :rules="{ required: true }"
            >
              <!-- <el-input v-model="item.inTime"></el-input> -->
              <el-date-picker
                v-model="item.timeStart"
                style="width: 240px"
                type="daterange"
                range-separator="至"
                start-placeholder="进入日期"
                end-placeholder="离开日期"
                format="yyyy 年 MM 月 dd 日"
                value-format="yyyy-MM-dd"
              />
            </el-form-item>
            <el-form-item
              label="效果"
              :prop="'dynamicItem.'+ index +'.effect'"
              :rules="{
                required: true,
                message: '名称不能为空',
                trigger: 'blur',
              }"
            >
              <el-input v-model="item.effect" style="width: 130px" />
            </el-form-item>
            <el-form-item>
              <i
                class="el-icon-check"
                style="margin-right: 20px"
                @click="handleAddTrain_1('from',item,index)"
              />
              <i class="el-icon-delete" @click="deleteItem(item, index)" />
            </el-form-item>
          </div>
        </el-form>
      </el-form>
      <!-- @click="handleUpdate('child')" -->
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="handleClose">取 消</el-button>
        <el-button
          v-if="editType == 2"
          type="primary"
          @click="handleUpdate('child')"
        >修 改</el-button>
        <el-button
          v-if="editType == 1"
          type="primary"
          @click="handleAddUser('child')"
        >添 加</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  addChild,
  deleteChild,
  updateChild,
  getAllChild,
  getChildById,
  getAllParent,
  addTrain,
  getTrainById,
  editTrain,
  deleteTrain
} from '@/api/child'
import { addFather, addMother } from '@/api/parent'
export default {
  data() {
    return {
      url: process.env.VUE_APP_BASE_API + 'file/',
      imageUrl: '',
      form: {
        orgName: '',
        effect: '',
        timeStart: [],
        dynamicItem: []
      },
      formRules: {
        name: [{ required: true, message: '请输入姓名', trigger: 'blur' }],
        phone: [
          { required: true, message: '请输入手机号', trigger: 'blur' },
          {
            pattern: /^1[34578]\d{9}$/,
            message: '目前只支持中国大陆的手机号码'
          }
        ]
      },
      sex: [{
        value: '0',
        label: '女'
      }, {
        value: '1',
        label: '男'
      }],
      have_bro: [
        {
          value: '0',
          label: '没有'
        }
      ],
      father: [],
      mother: [],
      String: [],
      currentPage: 1, // 默认显示页面为1
      pagesize: 5, //    每页的数据条数
      list: [],
      train: {},
      train_id: [],
      trainingInstitutionRecordId: '',
      listLoading: false,
      dialogFormVisible: false,
      is_add_father: false,
      is_add_mother: false,
      is_add_train: false,
      this_stuid: '',
      jurisdiction: [],
      editType: 1, // 1 添加 2 修改
      child: [],
      formLabelWidth: '200px',
      tableData: [
        {
          date: '2020-05-02',
          name: '新东方',
          effect: '解除抑郁症'
        },
        {
          date: '2016-05-04',
          name: '学大',
          effect: '成功戒烟'
        },
        {
          date: '2016-05-01',
          name: '望京医院',
          effect: '恢复了一半的自闭症'
        },
        {
          date: '2016-05-03',
          name: '北京医院',
          effect: '阿萨的驱动器'
        }
      ],
      rules: {
        childIdcard: [
          { required: true, message: '证件号不能为空', trigger: 'blur' }
        ],
        childMedicalInfo: [
          { required: true, message: '医疗信息不能为空', trigger: 'blur' }
        ],
        childName: [
          { required: true, message: '医疗信息不能为空', trigger: 'blur' }
        ],
        childSex: [
          { required: true, message: '孩子怎么会没有性别呢', trigger: 'blur' }
        ],
        childStatus: [
          { required: true, message: '学生状态不能为空', trigger: 'blur' }
        ],
        childTeachingInfo: [
          { required: true, message: '学生教育信息不能为空', trigger: 'blur' }
        ]
      }
    }
  },
  created() {
    this.fetchData()
    this.getparent()
  },
  methods: {
    handleAvatarSuccess(res, file) {
      this.imageUrl = res.obj.split(';')[0]
      this.child.studentHeadImages = this.imageUrl
    },
    beforeAvatarUpload(file) {
      // const isJPG = file.type === 'image/jpeg'
      const isImage = file.type === 'image/png' || file.type === 'image/jpg' || file.type === 'image/jpeg'
      const isLt2M = file.size / 1024 / 1024 < 2

      if (!isImage) {
        this.$message.error('上传图片只能是 JPG或PNG 格式!')
      }
      if (!isLt2M) {
        this.$message.error('上传图片大小不能超过 5MB!')
      }
      return isImage && isLt2M
      // return true
    },
    showSex(row, column) {
      if (row.studentSex === '1') {
        return '男'
      }
      if (row.studentSex === '0') {
        return '女'
      }
    },
    test_str() {
      // var allTrain = new Array()
      // for (let i = 0; i < this.train_id.length; i++) {
      //   allTrain.push(this.train_id[i])
      // }
      var allTrain = this.train_id.toString()
      console.log(allTrain)
      this.TrainingInstitutionRecords = allTrain.replace(/,/g, ';')
    },
    // --------------------------------------------动态增加input框---------------------------------------------
    addItem() {
      this.form.dynamicItem.push({
        orgName: '',
        timeStart: [],
        effect: ''
      })
    },
    deleteItem(item, index) {
      this.form.dynamicItem.splice(index, 1)
    },
    // -------------------------------------------------------------------------------------------------------
    getparent: function() {
      console.log(localStorage.getItem('this_staff_id'))
      getAllParent(1).then((resp) => {
        console.log('爸爸')
        this.father = resp.obj
        console.log(this.father)
      })

      getAllParent(0).then((r) => {
        console.log('妈妈')
        this.mother = r.obj
        console.log(this.mother)
      })
    },
    // 每页下拉显示数据
    handleSizeChange: function(size) {
      this.pagesize = size
    },
    // 点击第几页
    handleCurrentChange: function(currentPage) {
      this.currentPage = currentPage
    },
    handleClick(row) {
      console.log(row)
    },
    fetchData() {
      this.listLoading = true
      getAllChild().then((data) => {
        this.list = data.obj
        this.listLoading = false
      })
      getAllParent(1).then((f) => {
        this.father = f.obj
      })
      getAllParent(0).then((m) => {
        this.mother = m.obj
      })
    },
    addParent(sex, childId) {
      this.is_add_father = true
    },
    addF(sex, childId) {
      this.is_add_father = true
    },
    handleOpen(title, type, id) {
      this.child = []
      this.dialogFormVisible = true
      this.title = title
      this.editType = type
      if (type === 2) {
        console.log(id)
        this.this_stuid = id
        getChildById(id).then((resp) => {
          console.log(resp)
          this.child = resp.obj.student
          this.child.studentBrothers = String(this.child.studentBrothers)
          this.train = resp.obj.TrainingInstitutionRecords
          for (var i = 0; i < this.train.length; i++) {
            this.train_id.push(this.train[i].trainingInstitutionRecordId)
          }
          this.imageUrl = this.child.studentHeadImages
          console.log('这是孩子信息？', this.child)
          console.log('机构信息？', this.train)
        })
      } else {
        console.log('这是孩子信息？', this.child)
      }
    },
    addT(type) {
      this.editType = type
      this.is_add_train = true
    },
    addM(sex, childId) {
      this.is_add_mother = true
    },
    handleDelete(id, username) {
      this.$confirm('确认删除该学生？姓名为' + username).then(() =>
        deleteChild(id).then((data) => {
          this.$notify({
            title: '操作成功',
            type: 'success'
          })
          const totalPage = Math.ceil((this.list.length - 1) / this.pagesize)
          const page =
            this.currentPage > totalPage ? totalPage : this.currentPage
          this.currentPage = this.currentPage < 1 ? 1 : page
          this.fetchData()
        })
      )
    },
    handleAddTrain(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          this.train.studentId = this.this_stuid
          this.train.timeEndtoString = this.train.timeEnd
          this.train.timeStarttoString = this.train.timeStart
          addTrain(this.train).then((resp) => {
            this.$notify({
              title: '操作成功',
              type: 'success'
            })
            console.log(this.this_stuid)
            // getChildById(this.this_stuid)
            this.fetchData()
          })
          this.train = {}
          this.is_add_train = false
          this.this_stuid = ''
          return true
        } else {
          this.$notify({
            title: '请正确填写',
            type: 'error'
          })
          return false
        }
      })
    },
    handleAddTrain_1(formName, item, index) {
      console.log(this.form.dynamicItem[index])
      this.train = {}
      this.train.studentId = this.this_stuid
      this.train.timeEndtoString = this.form.dynamicItem[index].timeStart[1]
      this.train.timeStarttoString = this.form.dynamicItem[index].timeStart[0]
      this.train.effect = this.form.dynamicItem[index].effect
      this.train.orgName = this.form.dynamicItem[index].orgName
      this.train.staffId = localStorage.getItem('this_staff_id')
      addTrain(this.train).then((resp) => {
        console.log(resp)
        this.train_id.push(resp.obj)
        this.$notify({
          title: '操作成功',
          type: 'success'
        })
        this.fetchData()
      })
      console.log(this.train_id)
    },
    handleUpdate(formName) {
      var allTrain = this.train_id.toString()
      console.log(allTrain)
      this.child.studentTreatTrainingInstitutionRecords = allTrain.replace(/,/g, ';')
      this.child.studentBirthtoString = this.child.studentBirth
      this.child.studentJoinCentertoString = this.child.studentJoinCenter
      this.$refs[formName].validate((valid) => {
        if (valid) {
          console.log(this.child)
          updateChild(this.child).then((resp) => {
            console.log(resp)
            this.$notify({
              title: '修改成功',
              type: 'success'
            })
            this.fetchData()
            this.train_id = []
            this.form.dynamicItem = []
          })
          this.dialogFormVisible = false
          return true
        } else {
          this.$notify({
            title: '请正确填写',
            type: 'error'
          })
          return false
        }
      })
    },
    handleAddUser(formName) {
      var allTrain = this.train_id.toString()
      console.log(allTrain)
      this.child.studentTreatTrainingInstitutionRecords = allTrain.replace(/,/g, ';')
      this.child.studentBirthtoString = this.child.studentBirth
      this.child.studentJoinCentertoString = this.child.studentJoinCenter
      this.$refs[formName].validate((valid) => {
        if (valid) {
          // aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa需要得到train_id列表的id（）
          addChild(this.child).then((resp) => {
            this.$notify({
              title: '操作成功',
              type: 'success'
            })

            this.fetchData()
            this.train_id = []
            this.form.dynamicItem = []
          })
          this.child = []
          this.dialogFormVisible = false
          return true
        } else {
          this.$notify({
            title: '请正确填写',
            type: 'error'
          })
          return false
        }
      })
    },
    handleAddMother(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          addMother(this.mother).then((resp) => {
            this.$notify({
              title: '操作成功',
              type: 'success'
            })
            getAllParent(0)
            this.fetchData()
          })
          this.mother = {}
          this.is_add_mother = false
          return true
        } else {
          this.$notify({
            title: '请正确填写',
            type: 'error'
          })
          return false
        }
      })
    },
    handleAddFather(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          addFather(this.father).then((resp) => {
            this.$notify({
              title: '操作成功',
              type: 'success'
            })

            this.fetchData()
          })
          this.father = {}
          this.is_add_father = false
          return true
        } else {
          this.$notify({
            title: '请正确填写',
            type: 'error'
          })
          return false
        }
      })
    },
    handleDeleteTrain(id) {
      console.log('进了')
      deleteTrain(id).then((data) => {
        this.$notify({
          title: '操作成功',
          type: 'success'
        })
        this.fetchData()
      })
    },
    handleOpenTrain(title, type, id) {
      this.train = {}
      this.is_add_train = true
      this.title = title
      this.editType = type
      if (type === 2) {
        console.log(id)
        getTrainById(id).then((resp) => {
          console.log(resp)
          this.train = resp.obj
          console.log('机构信息啊！！', this.train)
        })
      }
    },
    handleClose_f() {
      this.is_add_father = false
      this.father = {}
    },
    handleClose_m() {
      this.is_add_mother = false
      this.mother = {}
    },
    handleClose_t() {
      this.is_add_train = false
      this.train = {}
    },
    handleClose() {
      this.dialogFormVisible = false
      this.train_id = []
      this.form.dynamicItem = []
      this.child = []
      this.imageUrl = ''
    },
    // handleUpdate(formName) {
    //   var allTrain = this.train_id.toString()
    //   console.log(allTrain)
    //   this.child.studentTreatTrainingInstitutionRecords = allTrain.replace(/,/g, ';')
    //   this.child.studentBirthtoString = this.child.studentBirth
    //   this.child.studentJoinCentertoString = this.child.studentJoinCenter
    //   this.$refs[formName].validate((valid) => {
    //     if (valid) {
    //       console.log(this.child)
    //       updateChild(this.child).then((resp) => {
    //         console.log(resp)
    //         this.$notify({
    //           title: '修改成功',
    //           type: 'success'
    //         })
    //         this.fetchData()
    //         this.train_id = []
    //         this.form.dynamicItem = []
    //       })
    //       this.dialogFormVisible = false
    //       return true
    //     } else {
    //       this.$notify({
    //         title: '请正确填写',
    //         type: 'error'
    //       })
    //       return false
    //     }
    //   })
    // },
    handleUpdateTrain(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          console.log(this.train)
          editTrain(this.train).then((resp) => {
            console.log(resp)
            this.$notify({
              title: '修改成功',
              type: 'success'
            })
            getChildById(this.this_stuid).then((resp) => {
              console.log(resp)
              this.child = resp.obj.student
              this.child.studentBrothers = String(this.child.studentBrothers)
              this.train = resp.obj.TrainingInstitutionRecords
              for (var i = 0; i < this.train.length; i++) {
                this.train_id.push(this.train[i].trainingInstitutionRecordId)
              }
              this.imageUrl = this.child.studentHeadImages
              console.log('---------------进来了！！----------------------------')
              console.log('机构信息？', this.train)
            })
            this.fetchData()
          })
          this.is_add_train = false
          this.train = {}
          return true
        } else {
          this.$notify({
            title: '请正确填写',
            type: 'error'
          })
          return false
        }
      })
    }
  }
}
</script>

<style scoped>
#main {
  margin-left: 5px;
  margin-right: 5px;
}
#table-area {
  margin-left: 10px;
  margin-right: 10px;
}
.avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}
.avatar-uploader .el-upload:hover {
  border-color: #409EFF;
}
.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 100px;
  height: 100px;
  line-height: 100px;
  text-align: center;
}
.avatar {
  width: 100px;
  height: 100px;
  display: block;
  border-radius:50%;
}
</style>
